package com.mbyte.easy.studentInfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.qcbyclass.entity.QcbyClass;
import com.mbyte.easy.qcbyclass.service.IQcbyClassService;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import com.mbyte.easy.status.entity.Status;
import com.mbyte.easy.status.service.IStatusService;
import com.mbyte.easy.student.entity.Student;
import com.mbyte.easy.studentInfo.entity.TStudent;
import com.mbyte.easy.studentInfo.mapper.TStudentMapper;
import com.mbyte.easy.studentInfo.service.ITStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-06-17
 */
@Service
public class TStudentServiceImpl extends ServiceImpl<TStudentMapper, TStudent> implements ITStudentService {
    @Autowired
    private TStudentMapper studentMapper;
    @Autowired
    private IQcbyClassService qcbyClassService;
    @Autowired
    private IStatusService statusService;
    @Autowired
    private IUserService userService;
    /**
     * 功能描述：删除学生信息
     * return：
     *
     * @param userId
     * @return
     */
    @Override
    public TStudent selectByUserId(Long userId) {
        return studentMapper.selectByUserId(userId);
    }

    @Override
    public IPage<TStudent> pageInfo(Page<TStudent> page, QueryWrapper<TStudent> queryWrapper,TStudent student) {
        IPage<TStudent> page1 = this.page(page, queryWrapper);
        List<TStudent> records = page1.getRecords();
        List<TStudent> records1 = new ArrayList<>();
        List<QcbyClass> classList = qcbyClassService.list();
        List<Status> statusList = statusService.list();
        boolean isHave = false;
        for (int i = 0; i < records.size(); i++) {
            TStudent tStudent = records.get(i);
            SysUser sysUser = userService.selectByPrimaryKey(tStudent.getUserId());
            tStudent.setUsername(sysUser.getUsername());
            tStudent.setSexType(sysUser.getSexType());
            for (int i1 = 0; i1 < classList.size(); i1++) {
                if(tStudent.getClassId() == classList.get(i1).getId()){
                    tStudent.setClassName(classList.get(i1).getClassName());
                }
            }
            for (int i1 = 0; i1 < statusList.size(); i1++) {
                if(tStudent.getSex() == statusList.get(i1).getId()){
                    tStudent.setStatusName(statusList.get(i1).getStatus());
                }
            }
            if(student.getUsername() != null && student.getUsername() != ""){
                isHave = true;
                if(student.getUsername().equals(tStudent.getUsername())){
                    records1.add(tStudent);
                    page1.setRecords(records1);
                    return page1;
                }
            }
        }
        page1.setRecords(records);
        if(isHave){
            page1.setRecords(records1);
        }
        return page1;
    }
}
